package com.iretailer.report.analyze.device.face.horizon;

import com.alibaba.fastjson.JSON;
import com.iretailer.report.analyze.annotation.AnalyzeClass;
import com.iretailer.report.analyze.commonUtil.Analyze;
import com.iretailer.report.analyze.commonUtil.ResponeResultType;
import com.iretailer.report.analyze.device.face.FaceInsert;
import com.iretailer.report.analyze.device.face.horizon.model.HorizonModel;
import com.iretailer.report.util.StringUtil;
import io.netty.buffer.ByteBuf;
import io.netty.handler.codec.http.HttpContent;
import io.netty.util.CharsetUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @Auther: xjw
 * @Date: 2019/8/6 0006 10:15
 * @Description:
 */
@AnalyzeClass
@Component
public class HorizonAnalyze implements Analyze {
    private static final Logger log = LoggerFactory.getLogger(HorizonAnalyze.class);


    @Value("${netty.face.horizon}")
    private String faceHorizon;
    @Autowired
    private FaceInsert faceInsert;
    @Autowired
    private FacePush facePush;

    @Override
    public String[] analyzePath() {
        if (StringUtil.notEmpty(faceHorizon)) {
            return faceHorizon.split(",");
        } else {
            return new String[]{"/openapi/v1/smart_data/upload", "/face", "/faceflow"};
        }
    }

    @Override
    public ResponeResultType nettyAnalyze(Map<String, Object> paramsMap, Object msg) {
        if (msg instanceof HttpContent) {
            HttpContent httpContent = (HttpContent) msg;
            ByteBuf byteBuf = httpContent.content();
            if (byteBuf != null) {
//                log.error("---- " + byteBuf.toString(CharsetUtil.UTF_8));
                String json = byteBuf.toString(CharsetUtil.UTF_8);
                log.info("receive json data:{}", json);
                HorizonModel horizonModel = JSON.parseObject(json, HorizonModel.class);
//                log.error(JSON.toJSONString(horizonModel.getProperties()));
                String deviceId = horizonModel.getProperties().getDeviceId();

//                log.error("deviceId {}  Alarm = {}", deviceId, JSON.toJSONString(horizonModel.getSmartData().getAlarm()));
//                log.error("Face = {}", JSON.toJSONString(horizonModel.getSmartData().getFace()));
//                log.error("Feature = {}", JSON.toJSONString(horizonModel.getSmartData().getFeature()));
//                log.error("deviceId {} flowStatus = {}", deviceId, JSON.toJSONString(horizonModel.getSmartData().getFlowStats()));
//                log.error("flowEvent = {}", JSON.toJSONString(horizonModel.getSmartData().getFlowEvent()));

                //
                String path = (String) paramsMap.get("path");
                log.error("deviceId {} path = {}", deviceId, path);
                // 如果 path 是 则只要客流数据
                switch (path) {
                    case "/alarmface":
                        // 报警
                        facePush.faceAlarm(horizonModel);

                    case "/face":
                        // 推送
//                        facePush.addPushMap(horizonModel);
//                        horizonModel.getSmartData().setFace(null);
//                        horizonModel.getSmartData().setFeature(null);
//                        horizonModel.getSmartData().setFlowEvent(null);
                        break;
                    case "/faceflow":
                        // 推送
//                        facePush.addPushMap(horizonModel);
//                        horizonModel.getSmartData().setFace(null);
//                        horizonModel.getSmartData().setFeature(null);
//                        horizonModel.getSmartData().setFlowEvent(null);
                        break;
                }

                return faceInsert.insertHorizon(horizonModel);
            } else {
                log.error("----- ByteBuf is null. {}", paramsMap.get("remoreAddr"));
                return ResponeResultType.getBadRequest(ResponeResultType.CFLType.CLOSE);
            }
        } else {
            return ResponeResultType.getBadRequest(ResponeResultType.CFLType.CLOSE);
        }
    }


    @Override
    public ResponeResultType springAnalyze(Map<String, Object> paramsMap, Object msg) {
        return null;
    }


}
